const { createCanvas, loadImage } = require('canvas');
const fs = require("fs");
const readline = require('readline');
const filename = "0001rawyolo";
let ocrData = require('./ocr.json');
(
    async () => {
        let image = await loadImage('./' + filename + '.jpg')
        const canvas = createCanvas(image.naturalWidth, image.naturalHeight)
        const ctx = canvas.getContext('2d')
        ctx.drawImage(image, 0, 0, image.naturalWidth, image.naturalHeight)
        ctx.beginPath();
        ctx.lineWidth = "0.5";
        ctx.strokeStyle = "blue";
        ctx.font="15px Microsoft YaHei";

        // let res = fs.createReadStream(path.join(__dirname, targetFolder, filelist[i]))
        // let res = fs.createReadStream('./' + filename + '.txt')
        // // console.log(res)
        // const rl = readline.createInterface({
        //     input: res, //or fileStream 
        //     output: res
        // });
        // let res = await fs.readFileSync('./train/labels_with_ids/S1001.txt', "utf8")
        let ocrele = ocrData.words_result
        for await (const line of ocrele) {
            let bbox = line.location
            let x_width = +bbox.width
            let y_width = +bbox.height
            ctx.rect(bbox.left, bbox.top, x_width, y_width);
            ctx.strokeText(line.words,bbox.left, bbox.top,500)
            ctx.stroke();
        }
        fs.writeFileSync(filename + "pro.jpg", canvas.toBuffer(), "binary");
    }
)()
